﻿/****************************** Module Header ******************************\
 * Module Name:  JqCombo.cs
 * Module Version:  2.0.0 (2012-7-11)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for pick the catalog.
 * Note: 从第一版的CatalogComboBox,到第二版的TreeListComboBox,到这个JqCombo,
 *  开发一个自己满意的分类下拉选择框一直是自己不断追求的一个目标，而今总算
 *  能暂时舒缓一口气了。以后不用再看着分类选择框纠结了哈。(天澜.2012-7-11)
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UCL.Web.CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:JqCombo runat=server></{0}:JqCombo>")]
    public class JqCombo : CompositeControl
    {

        private TextBox tbText;
        private HiddenField hidValue;

        /// <summary>
        /// 获取或设置控件的值
        /// </summary>
        public string Value
        {
            get
            {
                EnsureChildControls();
                return this.hidValue.Value;
            }
            set
            {
                EnsureChildControls();
                this.hidValue.Value = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的文本
        /// </summary>
        public string Text
        {
            get
            {
                EnsureChildControls(); 
                return this.tbText.Text;
            }
            set
            {
                EnsureChildControls(); 
                this.tbText.Text = value;
            }
        }

        /// <summary>
        /// WADS相对路径
        /// <para>如：/WADS (路径末尾不要加斜杠)</para>
        /// </summary>
        public string WADSPath { get; set; }

        /// <summary>
        /// 获取或设置控件的数据
        /// </summary>
        public string JsonData { get; set; }

        /// <summary>
        /// 设置控件的控制选项
        /// </summary>
        public string Options { get; set; }

        /// <summary>
        /// 创建子控件
        /// </summary>
        protected override void CreateChildControls()
        {
            this.tbText = new TextBox();
            this.hidValue = new HiddenField();
            if (this.CssClass != null) tbText.CssClass = this.CssClass;
            this.Controls.Add(this.tbText);
            this.Controls.Add(this.hidValue);
        }

        /// <summary>
        /// 预绘制处理
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            Page.PreRenderComplete += PagePreRenderComplete;
            base.OnPreRender(e);
        }

        /// <summary>
        /// 页面重绘完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PagePreRenderComplete(object sender, EventArgs e)
        {
            EnsureChildControls();
            Options = "{0}{1}height:'240',width:'210'".Fmt(string.IsNullOrEmpty(Options) ? "" : Options.Trim(','), string.IsNullOrEmpty(Options) ? "" : ",");
            if (!string.IsNullOrEmpty(Value)) Options = "{0},value:'{1}'".Fmt(Options,Value);
            if (string.IsNullOrEmpty(WADSPath)) WADSPath = "/WADS";

            if (string.IsNullOrEmpty(JsonData))
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, "jQuery(document).ready(function($){{$.post('{3}/Apis/apiGetCatalogData.ashx',{{}},function(d){{$('input[name=\"{0}\"]').JqComboBox({{{1},data:eval(d),valueControl:$('input[name=\"{2}\"]')[0]}})}})}});".Fmt(tbText.UniqueID, Options, hidValue.UniqueID,WADSPath), true);
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, "jQuery(document).ready(function($){{$('input[name=\"{0}\"]').JqComboBox({{{1},data:{3},valueControl:$('input[name=\"{2}\"]')[0]}});}});".Fmt(tbText.UniqueID, Options,hidValue.UniqueID,JsonData), true);
            }
            this.Page.ClientScript.RegisterClientScriptInclude("JqCombo", "{0}/Skin/JqPlus/JqComboBox/JqComboBox.js".Fmt(WADSPath));
        }

        /// <summary>
        /// 重绘控件外观
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            this.tbText.RenderControl(writer);
            this.hidValue.RenderControl(writer);
        }
    }
}
